package cn.train.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.train.dao.IUserTableDao;
import cn.train.model.Contable;
import cn.train.model.UserTable;
import cn.train.util.SqlSessionFactoryUtil;





public class UserTableService {
	SqlSession session=SqlSessionFactoryUtil.getSqlSession();
	IUserTableDao usertableDao = session.getMapper(IUserTableDao.class);
	
	public int getCount() {
		return usertableDao.getCount();
	}
	
	public List<UserTable> getUserByPage(int pageIndex,int pageSize){
		pageIndex=(pageIndex-1)*pageSize;
		return usertableDao.getUserByPage(pageIndex, pageSize);
	}
	
	public boolean updateUserTab(UserTable usertable) {
		boolean flag=usertableDao.updateUserTab(usertable);
		if(flag) {
		session.commit();
		}
		return flag;
	}
	public UserTable getUser(String uname) {
		
		return usertableDao.getUser(uname);
	}
	public List<UserTable> getAll(){
		return usertableDao.getAll();
	}
	@Test
	public void test(){
		List<UserTable> list=usertableDao.getAll();
		for (UserTable u : list) {
			System.out.println(u.getUname());
		}
	}
	public boolean addUser(UserTable usertable) {
		boolean flag=usertableDao.addUser(usertable);
		if(flag) {
			session.commit();			
		}
		return flag;
		
	}
	
	public UserTable login(String uname, String upass) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("uname", uname);
		map.put("upass", upass);
		UserTable usertable = usertableDao.login(map);
		return usertable;
	}
	
	public boolean updatexinxi(UserTable usertable) {
		boolean flag=usertableDao.updatexinxi(usertable);
		if(flag) {
		session.commit();
		}
		return flag;
	}
	public boolean findPass(String name,String question,String pass){
		boolean flag=usertableDao.findPass(name, question, pass);
		if(flag){
			session.commit();
			return true;
		}else{
			return false;
		}
	}
	public List<Contable> getContable(int uid){
		return usertableDao.getContable(uid);
	}

}
